import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @author: macy
 * @Email: 420534221@qq.com
 * @Date: 2020/6/16
 * @describe:
 */
public class Test {


    //https://admin.yespmp.com/YespmpWeb/getUser?pageIndex=1
    public static List<String> result=new ArrayList<String>();
    public static Map<Integer,String> keyMap=new HashMap<Integer, String>();

    public static  void main(String[] areg) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(30);

        for (int i=1;i<2084;i++){//2084
            pool.execute(new MyRunnable(i));
        }

        //pool.shutdown();

        pool.shutdown();
        boolean isFlag=true;
        while (isFlag){
            if (pool.isTerminated()){
                //具体操作
                isFlag=false;

                sortMapByKey(keyMap);
                for (Integer key : keyMap.keySet()) {
                    result.add(keyMap.get(key));
                }

                writeDataHubData(result);

            }
        }


    }



    static class MyRunnable implements Runnable{
        int index=1;
        public MyRunnable(int index){
            this.index=index;
        }

        public void run() {
            Map params = new HashMap();
            params.put("pageIndex",index);
            String str = HttpUtils.get("https://admin.yespmp.com/YespmpWeb/getUser", params, 3000, 3000, "UTF-8");
            keyMap.put(index,str);
            System.out.println("第"+index+"页加载完成...");
        }
    }

    private static Map<Integer,String> sortMapByKey(Map<Integer,String > map) {
        //treeMap适用于按自然顺序或自定义顺序遍历键(key)
        Map<Integer,String> treeMap = new TreeMap<Integer,String>(new MapKeyComparator());
        treeMap.putAll(map);
        return treeMap;
    }

    /**
     * 自定义比较器
     */
    static class MapKeyComparator implements Comparator<Integer>{
        @Override
        public int compare(Integer str1, Integer str2) {
            return str2.compareTo(str1);
        }
    }

    /**
     * 写入txt文件
     *
     * @param result
     * @return
     */
    public static boolean writeDataHubData(List<String> result) {
        long start = System.currentTimeMillis();
        String filePath = "D:\\data\\txt";
        String fileName="myname";

        StringBuilder content = new StringBuilder();
        boolean flag = false;
        BufferedWriter out = null;
        try {
            if (result != null && !result.isEmpty() && StringUtils.isNotEmpty(fileName)) {
                fileName +=".txt";
                File pathFile = new File(filePath);
                if (!pathFile.exists()) {
                    pathFile.mkdirs();
                }
                String relFilePath = filePath + File.separator + fileName;
                File file = new File(relFilePath);
                if (!file.exists()) {
                    file.createNewFile();
                }
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "GBK"));
                for (String info : result) {
                    out.write(info);
                    out.newLine();
                    out.newLine();
                }
                flag = true;


                System.out.println("写入文件耗时：*********************************" + (System.currentTimeMillis() - start) + "毫秒");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return flag;
        }
    }

}
